<?php
// +----------------------------------------------------------------------
// | Description: Address management
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-31 13:40:28
// +----------------------------------------------------------------------

namespace app\client\controller;

use app\common\controller\Rest;
use app\client\model\Member;

class Address extends Rest
{
    public $modelName = 'Address';
    public function _initialize()
    {
        parent::_initialize();
        // $this -> abort(!$this -> isClient, 'Permission denied', 403);
    }

    public function save()
    {
        $model = self::$model;
        $res = $model -> createData($this->param);
        $this->abort($res, self::$model->getError());
        $lastinsertId = $model -> getLastInsID();
        if($lastinsertId && $this -> param['Checked']){
            $member = new Member();
            $member -> updateDataById(['AddressId' => $lastinsertId], $this -> param['UserId']);
        }
        $this -> jsonData('添加成功');
    }

    public function index()
    {
        $data = self::$model->getDataList();
        $member = new Member();
        $AddressId = $member -> get($this -> param['UserId'])['AddressId'];
        foreach($data['list'] as $k => $v){
            if($v['Id'] === $AddressId){
                $data['list'][$k]['Checked'] = 1;
            }else{
                $data['list'][$k]['Checked'] = 0;
            }
        }
        $this->jsonData($data, self::$model -> getError());
    }
}